package com.example.restatr.Atcivity.JobSearch;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.JobSearch.Bean.JobBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class JobSearchAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private ListView jobList;
    public static List<JobBean.RowsBean> jobRowsBeans = new ArrayList<>();
    public int pos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_search_at);

        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        jobList = findViewById(R.id.jobList);
        Tools.SetBarTitle(tb, barTitle, "找工作", search, this, true);
        tb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item1:
                        startActivity(new Intent(JobSearchAt.this, JobResumeAt.class));
                        break;
                    case R.id.item2:
                        startActivity(new Intent(JobSearchAt.this,JobDeliverList.class));
                        break;
                }
                return true;
            }
        });
        GetJob();
    }

    private void GetJob() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/job/post/list", MainActivity.sp.getString("token", ""));
            jobRowsBeans.addAll(new Gson().fromJson(token, JobBean.class).getRows());
            runOnUiThread(() -> {
                if (jobRowsBeans.size() > 0) {
                    jobList.setAdapter(new JobListAdapter());
                }
            });
        }).start();
    }
    //投递简历
    public void deliverResume(int pos, Context context) {
        new Thread(() -> {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("companyId", jobRowsBeans.get(pos).getCompanyId());
                jsonObject.put("money", jobRowsBeans.get(pos).getSalary());
                jsonObject.put("satrTime", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                jsonObject.put("postId", jobRowsBeans.get(pos).getProfessionId());
                jsonObject.put("postName", jobRowsBeans.get(pos).getName());
                String post = Tools.Post("/prod-api/api/job/deliver", MainActivity.sp.getString("token", ""), jsonObject.toString());
                JSONObject jx = new JSONObject(post);
                String msg = jx.getString("msg");
                int code = jx.getInt("code");
                runOnUiThread(() -> {
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public int getPos() {
        return pos;
    }

    class JobListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return jobRowsBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return jobRowsBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.job_item_layout, null);
            TextView professionName = convertView.findViewById(R.id.professionName);
            TextView salary = convertView.findViewById(R.id.salary);
            TextView need = convertView.findViewById(R.id.need);
            TextView companyName = convertView.findViewById(R.id.companyName);
            TextView applyFor = convertView.findViewById(R.id.applyFor);

            professionName.setText(jobRowsBeans.get(position).getName());
            salary.setText(jobRowsBeans.get(position).getSalary() + "元/月");
            need.setText(jobRowsBeans.get(position).getAddress());
            companyName.setText(jobRowsBeans.get(position).getCompanyName());
            applyFor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                deliverResume(position,JobSearchAt.this);
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(JobSearchAt.this, JobInfoAt.class);
                    intent.putExtra("professionName", jobRowsBeans.get(position).getProfessionName());
                    intent.putExtra("salary", jobRowsBeans.get(position).getSalary());
                    intent.putExtra("need", jobRowsBeans.get(position).getNeed());
                    intent.putExtra("contacts", jobRowsBeans.get(position).getContacts());
                    intent.putExtra("location", jobRowsBeans.get(position).getAddress());
                    intent.putExtra("obligation", jobRowsBeans.get(position).getObligation());
                    intent.putExtra("CompanyId", jobRowsBeans.get(position).getCompanyId());
                    pos=position;
                    startActivity(intent);
                }
            });
            return convertView;
        }
    }


}